package com.wesley.android.demo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SlidingDrawer;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.SlidingDrawer.OnDrawerOpenListener;

/**
 * This demo represent how to implement the SlidingDrawer widget.
 * 
 * There are two main important component 
 * 1. Sliding Handler - This is the one contains handler button that is always visible in the application. 
 * 2. Sliding Container - The object container that animate slide up/down to show the objects.
 * 
 * @author Wesley
 */
public class SlidingDrawerActivity extends Activity implements OnDrawerOpenListener, OnDrawerCloseListener{

	private Button slideHandleButton;
	private SlidingDrawer slidingDrawer;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sliding_drawer_demo);
		
		slideHandleButton = (Button) findViewById(R.id.slideHandleButton);
		slidingDrawer = (SlidingDrawer) findViewById(R.id.SlidingDrawer);

		slidingDrawer.setOnDrawerOpenListener(this);
		slidingDrawer.setOnDrawerCloseListener(this);
		
		Button btnShow = (Button) findViewById(R.id.btnShow);
		btnShow.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				slidingDrawer.animateOpen();
			}
		});
		

		Button btnHide = (Button) findViewById(R.id.btnHide);
		btnHide.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				slidingDrawer.animateClose();
			}
		});
	}
	
	@Override
	public void onBackPressed() {
		if(slidingDrawer.isOpened()) {
			slidingDrawer.animateClose();
			return;
		}
		super.onBackPressed();
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		outState.putBoolean("isShown", slidingDrawer.isOpened());
		super.onSaveInstanceState(outState);
	}
	
	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		boolean isShown = savedInstanceState.getBoolean("isShown");
		if(isShown) {
			slidingDrawer.open();
		}
	}

	@Override
	public void onDrawerOpened() {
		slideHandleButton.setBackgroundResource(R.drawable.openarrow);
	}

	@Override
	public void onDrawerClosed() {
		slideHandleButton.setBackgroundResource(R.drawable.closearrow);
	}

}
